package com.zcily.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zcily.demo.entity.dto.Result;
import com.zcily.demo.entity.form.RegisterForm;
import com.zcily.demo.entity.po.User;
import com.zcily.demo.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Controller
@Api(tags = "首页")
@Slf4j
public class IndexController {

    @Resource
    private UserService userService;

    @GetMapping({"", "/"})
    @ResponseBody
    @ApiOperation("应用首页")
    public String index() {
        return "Hello, World!";
    }

    @PostMapping("/register")
    @ResponseBody
    @ApiOperation("用户注册")
    public Result<?> login(@Validated @RequestBody RegisterForm form) {
        User user = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, form.getUsername()));
        // TODO: 2025/6/18 这里模拟用户注册
        return Result.ok("注册成功");
    }

    @GetMapping("/send_register_verification_code")
    @ResponseBody
    @ApiOperation("发送注册验证码")
    public Result<?> sendRegisterVerificationCode(@RequestParam("phoneNumber") String phoneNumber) {
        // TODO: 2025/6/18 这里模拟发送注册的验证码
        String verificationCode = RandomStringUtils.random(6, false, true);
        log.info("发送号码是：{}；生成的6位验证码是：{}", phoneNumber, verificationCode);
        return Result.ok("发送成功");
    }
}
